#include "threadPool.h"
#include "../log/utils/error_utils.h"

int thread_pool_init(thread_pool_t *thread_pool, int worker_count)
{
    if (thread_pool == NULL || worker_count <= 0)
    {
        return SYSTEM_ERR_INVALID_ARGUMENT;
    }

    // 初始化共享数据
    shared_data_t *shared_data = &thread_pool->shared_data;
    CALL_RET_AND_LOG("thread_pool_init: task_queue_init",
                     task_queue_init(&shared_data->task_queue),
                     TASK_QUEUE_CHECK_INIT);
    pthread_mutex_init(&shared_data->queue_mutex, NULL);
    pthread_cond_init(&shared_data->queue_cond, NULL);
    shared_data->exit_flag = 0;

    // 初始化工作线程列表
    worker_list_t *worker_list = &thread_pool->worker_list;
    CALL_RET_AND_LOG("thread_pool_init: worker_list_init",
                     worker_list_init(worker_list, worker_count),
                     WORKER_LIST_INIT_CHECK);

    // 创建工作线程
    CALL_RET_AND_LOG("thread_pool_init: make_workers",
                     make_workers(worker_list, shared_data),
                     MAKE_WORKERS_CHECK);

    return SYSTEM_ERR_OK;
}